
//作业1：  拷贝一个文件夹，考虑子文件
//File src=new File(“d:\\aaa\\src”);
//File desc=new FILE(“aaa\\dess”）；

import java.io.*;

public class Java004 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("test\\f1");
        File f2 = new File("test\\f2");

        CREATE(f1);
        COPY(f1,f2);
    }
    public static void CREATE(File f1) throws IOException {
        File fa = new File("test\\f1\\aaa");
        File fb = new File("test\\f1\\bbb");
        fa.mkdirs();
        fb.mkdirs();

        for(int i=0;i<12;i++){
            new File("test\\f1\\aaa\\"+i+".txt").createNewFile();
            new File("test\\f1\\aaa\\"+i+".doc").createNewFile();
            new File("test\\f1\\aaa\\"+i+".class").createNewFile();
            new File("test\\f1\\aaa\\"+i+".py").createNewFile();
        }//a中
        for(int i=0;i<5;i++){
            new File("test\\f1\\bbb\\"+i+".txt").createNewFile();
            new File("test\\f1\\bbb\\"+i+".doc").createNewFile();
        }//b中

         new File("test\\f1\\newFile.txt").createNewFile();

        OutputStream fos = new FileOutputStream("test\\f1\\newFile.txt");
        fos.write(97);
        fos.write(98);
        fos.write(99);
        fos.close();

    }

    public static void COPY(File f1,File f2) throws IOException {
        String files[] = f1.list();
        if(f1.isDirectory()){
            if(!f2.exists()){
                f2.mkdirs();//复制文件夹
            }
            for(String f : files){
                File newf1 = new File(f1,f);
                File newf2 = new File(f2,f);
                COPY(newf1,newf2);
            }
        }else {
            COPYFILE(f1,f2);//复制文件
        }


    }

    public static void COPYFILE(File f1,File f2) throws IOException {
        InputStream fis = new FileInputStream(f1);
        OutputStream fos = new FileOutputStream(f2);

        byte[] b = new byte[1024];
        int len=0;
        while((len=fis.read(b))!=-1){
            fos.write(b,0,len);
        }
        fis.close();
        fos.close();

    }
}
